﻿using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Collections.Generic;
using ParieurPro.DAL;
using ParieurPro.DAL.DatabaseModel;

namespace MyBet.Web.Services
{
    [ServiceContract(Namespace = "")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class GetEventScore
    {

        [Serializable()]
        public struct EventInformation
        {
            public int userScore;
            public int bestScore;
            public DateTime lastUpdate;
        }

        [OperationContract]
        public EventInformation GetEventInformation(Guid userId, int eventid, int customContestId)
        {
            EventInformation returnScore = new EventInformation();
            using (MyBetDBEntities dbContext = new MyBetDBEntities())
            {
                RankObject eventScore = RankObject.GetUserRankInDatabaseFromEvent(dbContext, customContestId, userId, eventid);
                returnScore.userScore = -1;
                returnScore.lastUpdate=DateTime.MinValue;
                if ((eventScore != null) && (eventScore.GoodScoreCount.HasValue))
                {
                    // Le classement a été mis à jour, sinon c'est que la ligne a seulement
                    // été créée pour la date de derniere MAJ
                    returnScore.userScore = eventScore.Score;                    
                }
                if ((eventScore != null) && (eventScore.LastBetUpdate.HasValue))
                {
                    returnScore.lastUpdate = eventScore.LastBetUpdate.Value;
                }
                returnScore.bestScore = EventObject.GetBestScore(dbContext, eventid, customContestId);
            }
            return returnScore;
        }

        //[OperationContract]
        //public int GetBestScore(int eventid, int customContestId)
        //{
        //    int score;
        //    using (MyBetDBEntities dbContext = new MyBetDBEntities())
        //    {
                
        //    }
        //    return score;
        //}

        /// <summary>
        /// Return list of events of a customcontest
        /// </summary>
        /// <param name="customContestId"></param>
        /// <returns></returns>
        [OperationContract]
        public List<EventObject> GetEventList(int customContestId)
        {
            List<EventObject> returnList;
            using (MyBetDBEntities dbContext = new MyBetDBEntities())
            {
                returnList = EventObject.GetEventsFromCustomContest(dbContext, customContestId);
            }
            return returnList;
        }

        /// <summary>
        /// Return list of events of a customcontest
        /// </summary>
        /// <param name="customContestId"></param>
        /// <returns></returns>
        [OperationContract]
        public List<EventObject> GetGlobalEventList(int championshipId, int seasonId)
        {
            List<EventObject> returnList;
            using (MyBetDBEntities dbContext = new MyBetDBEntities())
            {
                returnList = EventObject.GetEventsFromChampionshipId(dbContext, championshipId, seasonId);
            }
            return returnList;
        }

        /// <summary>
        /// Get matchs of an event
        /// </summary>
        /// <param name="eventId">Event id</param>
        /// <returns></returns>
        [OperationContract]
        public List<MatchObject> GetMatchsFromEvent(int eventId)
        {
            List<MatchObject> returnList;
            using (MyBetDBEntities dbContext = new MyBetDBEntities())
            {
                returnList = MatchObject.GetMatchsFromEvent(dbContext, eventId);
            }
            return returnList;
        }

        /// <summary>
        /// Get matchs of an event
        /// </summary>
        /// <param name="eventId">Event id</param>
        /// <param name="openDate">Date à laquelle l'événement devient l'événement par défaut</param>
        /// <param name="closeDate">Date de fermeture des paris</param>
        /// <returns></returns>
        [OperationContract]
        public bool SetEventDates(int eventId, DateTime openDate, DateTime closeDate)
        {
            bool returnValue=false;
            using (MyBetDBEntities dbContext = new MyBetDBEntities())
            {
                try
                {
                    returnValue = EventObject.SetEventDates(dbContext, eventId, openDate, closeDate);
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Trace.WriteLine(ex.ToString());
                    returnValue = false;
                }
            }
            return returnValue;
        }

    }
}
